﻿using Single.Json;

namespace Single.Core.Service
{
    /// <summary>
    /// 服务结果
    /// </summary>
    public class ServiceResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 文本消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 数据对象
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="success">是否成功</param>
        /// <param name="message">文本消息</param>
        /// <param name="data">数据对象</param>
        public ServiceResult(bool success, string message = null, object data = null)
        {
            Success = success; Message = message; Data = data;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns>序列化对象字符串</returns>
        public string Serialize(string[] retain = null, string[] ignore = null, IDictionary<string, string> turns = null, string dateTimeFormate = "yyyy'-'MM'-'dd' 'HH':'mm':'ss")
        {
            return JsonHelper.Serialize(new { success = Success, message = Message, data = Data }, retain, ignore, turns, dateTimeFormate);
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns>序列化对象字符串</returns>
        public static string Serialize(bool success, string message = null, object data = null, string[] retain = null, string[] ignore = null, IDictionary<string, string> turns = null, string dateTimeFormate = "yyyy'-'MM'-'dd' 'HH':'mm':'ss")
        {
            return new ServiceResult(success, message, data).Serialize(retain, ignore, turns, dateTimeFormate);
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns>序列化对象字符串</returns>
        public static string SerializeData(object data, string[] retain = null, string[] ignore = null, IDictionary<string, string> turns = null, string dateTimeFormate = "yyyy'-'MM'-'dd' 'HH':'mm':'ss")
        {
            return new ServiceResult(true, string.Empty, data).Serialize(retain, ignore, turns, dateTimeFormate);
        }
    }
}